home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / LABEL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  10.4 KB  |  361 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.output.style_manager;
  4. import sub_arctic.output.color_pair;
  5. import sub_arctic.output.drawable;
  6. import java.awt.Font;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9.  
  10. /**
  11.  * This is a class which draws a string on the screen. This object has
  12.  * options for putting a box around the label, making it be opaque,
  13.  * setting its colors, and of course controlling the displayed string. 
  14.  * 
  15.  * @author Scott Hudson
  16.  */
  17. public class label extends oneline_text_display {
  18.  
  19.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  20.  
  21.   /** Are we drawing an opaque background? */
  22.   protected boolean _opaque = false;
  23.  
  24.   /** 
  25.    * Are we drawing an opaque background? 
  26.    * @return boolean true if the background is cleared before drawing
  27.    */
  28.   public boolean opaque() {return _opaque;}
  29.  
  30.   /** 
  31.    * Set whether we are drawing an opaque background. 
  32.    * @param boolean v the new opacity truth
  33.    */
  34.   public void set_opaque(boolean v) 
  35.     {
  36.       if (v != _opaque)
  37.     {
  38.           _opaque = v;
  39.       damage_self();
  40.     }
  41.     }
  42.  
  43.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  44.  
  45.   /**
  46.    * This stores the above spacing of the interactor. By default it
  47.    * is three pixels, just like online_text_display.
  48.    */
  49.   protected int _above_spacing=3;
  50.  
  51.   /**
  52.    * Get the spacing above the text of this interactor. Since this
  53.    * interactor has above and below spacing, the v_spacing attribute
  54.    * of its superclass is ignored.
  55.    *
  56.    * @return int the number of pixels above this interactor
  57.    */
  58.   public int above_spacing() { return _above_spacing;}
  59.  
  60.   /*
  61.    * Set the spacing above this text of this interactor. This 
  62.    * function and set_below_spacing() supersede the use of set_v_spacing
  63.    * on this interactor.
  64.    * @param int as the new spacing above the interactor
  65.    */
  66.   public void set_above_spacing(int as) {
  67.     _above_spacing=as;
  68.     calculate_size();
  69.   }
  70.  
  71.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  72.  
  73.   /**
  74.    * This stores the below spacing of the interactor. By default it
  75.    * is three pixels, just like online_text_display.
  76.    */
  77.   protected int _below_spacing=3;
  78.  
  79.   /**
  80.    * Get the spacing below the text of this interactor. Since this
  81.    * interactor has above and below spacing, the v_spacing attribute
  82.    * of its superclass is ignored.
  83.    *
  84.    * @return int the number of pixels below this interactor
  85.    */
  86.   public int below_spacing() { return _below_spacing;}
  87.  
  88.   /*
  89.    * Set the spacing below this text of this interactor. This 
  90.    * function and set_above_spacing() supersede the use of set_v_spacing
  91.    * on this interactor.
  92.    * @param int as the new spacing above the interactor
  93.    */
  94.   public void set_below_spacing(int bs) {
  95.     _below_spacing=bs;
  96.     calculate_size();
  97.   }
  98.  
  99.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  100.  
  101.   /**
  102.    * When this object changes its spacing attributes, we may need
  103.    * recalculate its intrinsic size.  This routine does that.
  104.    */
  105.   public void calculate_size() {
  106.     /* if we are autosizing, nobody cares */
  107.     if (!_autosize) return;
  108.     /* height of the font + the above and below */
  109.     set_intrinsic_h(_metric.getAscent() + _above_spacing +
  110.             _below_spacing);
  111.   }
  112.  
  113.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  114.  
  115.   /** Color pair that we draw with. */
  116.   protected color_pair _draw_colors = manager.default_color_pair();
  117.  
  118.   /** 
  119.    * Retrieve the color pair that we draw with. 
  120.    * @return color_pair the current color pair we are using
  121.    */
  122.   public color_pair draw_colors() {return _draw_colors;}
  123.  
  124.   /** 
  125.    * Set the color pair that we draw with. 
  126.    * @param color_pair c the new color pair to use
  127.    */
  128.   public void set_draw_colors(color_pair c) 
  129.     {
  130.       if (c != null)
  131.     {
  132.           _draw_colors = c;
  133.       damage_self();
  134.     }
  135.     }
  136.  
  137.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  138.  
  139.   /**
  140.    * Make a label using some simple defaults. It doesn't try to position 
  141.    * itself. It assumes you'll do this with constraints. It sizes by
  142.    * the size of the text.
  143.    *
  144.    * @param String s the string to put in the label
  145.    */
  146.   public label(String s) {
  147.     super(0,0,s);
  148.     set_boxed(false);
  149.   }
  150.  
  151.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  152.  
  153.   /** 
  154.    * Make a label with some defaults. This time you supply the
  155.    * width. Assumes x and y will use constraints. 
  156.    * 
  157.    * @param String s the string to display 
  158.    * @param int width the width of the displayable area in pixels
  159.    */
  160.   public label(String s, int width) {
  161.     super(0,0,width,s,style_manager.default_font(),true);
  162.     set_boxed(false);
  163.   }
  164.  
  165.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  166.  
  167.   /** 
  168.    * Make a label with some defaults. This time you supply the
  169.    * width & a font. Assumes x and y will use constraints.
  170.    * 
  171.    * @param String s the displayed string
  172.    * @param int width the number of pixels wide the display area is
  173.    * @param Font f the font render the string in
  174.    */
  175.   public label(String s, int width, Font f) {
  176.     super(0,0,width,s,f,true);
  177.     set_boxed(false);
  178.   }
  179.  
  180.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  181.  
  182.   /** 
  183.    * Make a label with some defaults. This time you supply the
  184.    * font. Assumes x and y will use constraints.
  185.    *  
  186.    * @param String s the displayed string
  187.    * @param Font f the font to use for rendering the string
  188.    */
  189.   public label(String s, Font f) {
  190.     super(0,0,s,f,true);
  191.     set_boxed(false);
  192.   }
  193.  
  194.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  195.  
  196.   /** 
  197.    * Draw the display 
  198.    * @param drawable d the drawable on which to do the display
  199.    */
  200.   protected void draw_self_local(drawable d) 
  201.     {
  202.       int x,y;
  203.       
  204.       /* clear the background if they asked for that */
  205.       if (opaque())
  206.     {
  207.           d.setColor(draw_colors().background());
  208.           d.fillRect(0,0,w()-1,h()-1);
  209.     }
  210.  
  211.       /* set font to our font, and start drawing in foreground */
  212.       d.setFont(font());
  213.       d.setColor(draw_colors().foreground());
  214.  
  215.       /* draw the text */
  216.       if (_autosize) {
  217.     d.drawString(text(), _h_spacing, _above_spacing+_metric.getAscent());
  218.       } else {
  219.     x=(w()-_metric.stringWidth(text()))/2;
  220.     y=(h()-(_metric.getHeight() - _metric.getLeading()))/2;
  221.     d.drawString(text(),x,y+_metric.getAscent());
  222.       }
  223.  
  224.       /* draw box if requested */
  225.       if (boxed())
  226.         d.drawRect(0,0,w()-1,h()-1);
  227.     }
  228.  
  229.    //had:
  230.    //* @exception general PROPAGATED
  231.  
  232.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  233.  
  234.   /**
  235.    * We override the implementation of set_text() for this object and 
  236.    * reset the width. The reason we override this is because the parent
  237.    * class oneline_text_display doesn't want to resize every time its
  238.    * text changes... that would be distracting. Label's however do 
  239.    * want to resize every time.
  240.    *  
  241.    * @param String t the string that is being displayed
  242.    */
  243.   public void set_text(String t) { 
  244.     super.set_text(t);
  245.     if (_autosize) {
  246.       set_w(_metric.stringWidth(text()) + 2*_h_spacing);
  247.     } 
  248.       damage_self();
  249.   }
  250.  
  251.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  252.  
  253.   /**
  254.    * This is the storage for the autosize variable.
  255.    */
  256.   protected boolean _autosize=true;
  257.  
  258.   /**
  259.    * Retrieve the current state of autosize.
  260.    * @return boolean true if the object is autosized (and thus not resizable)
  261.    */
  262.   public boolean autosize() { return _autosize;}
  263.  
  264.   /**
  265.    * Set the current state of the autosize variable. If you change the
  266.    * state from non-autosized (false) to autosized (true) the label
  267.    * will immediately autosize itself. If you change the state to
  268.    * non-autosized from autosized, no immediate redraw is requested
  269.    * and the object will continue at its current size unless that 
  270.    * is changed.
  271.    */
  272.   public void set_autosize(boolean a) {
  273.     Dimension d;
  274.  
  275.     /* no work to do */
  276.     if (a==_autosize) {
  277.       return;
  278.     }
  279.     /* save it */
  280.     _autosize=a;
  281.     /* is it currently false and now true? */
  282.     if (a==true) {
  283.       d=natural_size();
  284.       set_h(d.height);
  285.       set_w(d.width);
  286.     }
  287.   }
  288.  
  289.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  290.  
  291.   /**
  292.    * We override the intrinsic constraint function because sometimes
  293.    * we have intrinsic constraints (when autosize is on) and sometimes
  294.    * we don't. 
  295.    * @return int a constant representing what (if any) our intrinsic 
  296.    *               constraints are.
  297.    */
  298.   public int intrinsic_constraints() { 
  299.     if (_autosize) {
  300.       return H; 
  301.     } else {
  302.       return 0;
  303.     }
  304.   }
  305.  
  306.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  307.  
  308.   /**
  309.    * This function computes the natural size of a label. This is useful
  310.    * when you want to walk down a list (or more likely, column) of labels
  311.    * and find out which of them has the largest natural size and then
  312.    * set all of them to that size.
  313.    * @return Dimension the size (in pixels) of this button at its natural size
  314.    */
  315.   public Dimension natural_size() {
  316.     int height=(_metric.getHeight()-_metric.getLeading()) + (2*_v_spacing);
  317.     int width=(_metric.stringWidth(text()))+ (2*_h_spacing);
  318.     return new Dimension(width,height);
  319.   }
  320.  
  321.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  322.  
  323.   /**
  324.    * We have to override this to avoid getting a problem when the 
  325.    * superclass tries to set the intrinsic height. We don't bother
  326.    * setting the intrinsic height when we are not autosized.
  327.    *
  328.    * @param int h the new intrinsic height
  329.    */
  330.   public void set_intrinsic_h(int h) {
  331.     if (_autosize) {
  332.       super.set_intrinsic_h(h);
  333.     } else {
  334.       /* normal set h */
  335.       set_h(h);
  336.     }
  337.   }
  338.  
  339.    //had:
  340.    //* @exception cannot_assign PROPAGATED
  341.    //* @exception general PROPAGATED
  342.  
  343.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  344. }
  345. /*=========================== COPYRIGHT NOTICE ===========================
  346.  
  347. This file is part of the subArctic user interface toolkit.
  348.  
  349. Copyright (c) 1996 Scott Hudson and Ian Smith
  350. All rights reserved.
  351.  
  352. The subArctic system is freely available for most uses under the terms
  353. and conditions described in 
  354.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  355. and appearing in full in the lib/interactor.java source file.
  356.  
  357. The current release and additional information about this software can be 
  358. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  359.  
  360. ========================================================================*/
  361.